######### Make some plots from the Under Log BRT Fitted Values

#### Establish out directory

out.dir = '/home1/99/jc152199/underlogdownscale/BRTtc10/'

##### Read in model data

md = read.csv(paste(out.dir,'UL_BRT_Model_Data_Plus_Preds.csv',sep=''), header=T)

####### Plot up fitted vs training values

#### Configure plot space

png(paste(out.dir,'EmpVsPred.png',sep=''), units='cm', height=12, width=12, res=1000)

#### Establish plotting limits

lims = range(c(md$ulmax,md$max_preds),na.rm=T)

#### Plot points

plot(md$ulmax, md$max_preds, col='#FF000025', xlim=lims, ylim=lims, ylab='Predicted UL Temp', xlab='Empirical UL Temp', main='', pch=16, cex=.8)

#### Plot other points

points(md$ulmax[which(abs(md$ulmax-md$max_preds)>3.5)],md$max_preds[which(abs(md$ulmax-md$max_preds)>3.5)],col='black', pch=16, cex=.8)

#### 1:1 line

abline(a=0,b=1, lty=2, lwd=.9, col='black')

### LM

lm1 = lm(md$max_preds~md$ulmax) # Perform a linear model using the Empirical Data as y-values and the preds (AWAP or microCLIM) as x-values

#### Plot lm relationship

abline(lm1,col='blue',lty=2, lwd=.9)

#### Legend

legend('topleft',legend = c(paste('Adj. r^2 ',substr(summary.lm(lm1)[9],1,4),sep=''),paste('Slope ',round(lm1$coefficients[2],2),sep=''),paste('Intercept ',round(lm1$coefficients[1],2),sep='')),text.col=c('blue','blue','blue'), bty='n')
	
#### Shut device
	
dev.off()

#### Find high points with high residuals and remove them

md2 = md[which(abs(md$ulmax-md$max_preds)<3.75),]

#### Write out this dataframe and rerun the model on it

write.csv(md2,file='/home1/99/jc152199/underlogdownscale/ModelDataforULBRTResidOutliersRemoved.csv',row.names=F)